package page

import "dev.ixmxm.top/rms/utils/log"

type Params struct {
	PageSize  int64 `form:"page_size"`  //每页记录数量
	PageIndex int64 `form:"page_index"` //当前页码
}

//NewPage 根据输入生成page，一般由于数据库操作时用了offset等操作，total和len(data)不一致
func NewPageResult(pageSize, pageIndex, totalNumber int64) PageResult {
	return PageResult{
		PageSize:    pageSize,
		PageIndex:   pageIndex,
		TotalNumber: totalNumber,
		TotalPage:   GetTotalPages(totalNumber, pageSize),
	}
}

type PageResult struct {
	PageIndex   int64 //当前页码
	PageSize    int64 //每页记录数量
	TotalNumber int64 //总记录数
	TotalPage   int64 //总页数
}

type PageResultExample struct {
	PageIndex   int64         `json:"page_index"`   //当前页码
	PageSize    int64         `json:"page_size"`    //每页记录数量
	TotalNumber int64         `json:"total_number"` //总记录数
	TotalPage   int64         `json:"total_page"`   //总页数
	List        []ListExample `json:"list"`         //数据集合
}
type ListExample struct {
}

func (p PageResult) Json(data interface{}) map[string]interface{} {
	return map[string]interface{}{"page_size": p.PageSize, "page_index": p.PageIndex, "total_number": p.TotalNumber, "total_page": p.TotalPage, "list": data}
}

func GetTotalPages(totalNumber, pageSize int64) int64 {
	if totalNumber < 0 {
		log.Error("get total pages failed,total < 0")
		return 0
	}
	if pageSize == 0 {
		log.Error("get total pages failed,per page is 0")
		return 0
	}
	if totalNumber%pageSize == 0 {
		return totalNumber / pageSize
	}
	return totalNumber/pageSize + 1
}
